package com.kinstalk.voip.jni;

import com.kinstalk.voip.sdk.common.Log;

public class NVDataRender {

	public static final int TEXTURE_TYPE_Y = 0;
	public static final int TEXTURE_TYPE_U = 1;
	public static final int TEXTURE_TYPE_V = 2;

	private int mheight = 0;
	private int mwidth = 0;
	private boolean need_glTex_init = true;
//	private byte[]  current_stream = new byte[1280*720*3];
	private byte[]  current_stream = new byte[320*240*3];
	private boolean change_to_normal = false;
	public void setNormalShow(boolean flag) {
		change_to_normal = flag;
	}
	public  boolean DrawInit() {
		need_glTex_init = true;
		return true;
	}
	
	public boolean DrawFinish() {
		ClearMemory(current_stream);
	    need_glTex_init = true;
		return true;
	}
	
	public synchronized  boolean Draw() {
		boolean flag = false;
		long source = ConvertArry2Long(current_stream);
		flag = DrawData(source, mwidth, mheight, need_glTex_init);
		need_glTex_init = false;
		return flag;
	}
	//write by spy
	public synchronized  boolean DrawByType(int texturetype) {
		boolean flag = false;
		long source = ConvertArry2Long(current_stream);
		flag = DrawByType(source, mwidth, mheight, texturetype);
		return flag;
	}

	public synchronized  boolean ConvertNV212YUV(byte[] data, int width, int height) {
		if(mheight != height || mwidth != width)
	    {
	        need_glTex_init = true;
	        mheight = height;
	        mwidth = width;
	    }
		boolean flag = false;
		long input = ConvertArry2Long(data);
		if(change_to_normal) {
			Convert2Normal(input, mwidth, mheight);
		}
		long output = ConvertArry2Long(current_stream);
		flag = ConvertNV21(input, output, mwidth, mheight);
		
		return flag;
	}
	
	public synchronized  boolean Convert420P2YUV(long pointer , int width, int height) {
		if(mheight != height || mwidth != width)
	    {
	        need_glTex_init = true;
	        mheight = height;
	        mwidth = width;
	    }
		boolean flag = false;

		long output = ConvertArry2Long(current_stream);
		flag = ConvertYuv(pointer, output, mwidth, mheight);
		return flag;
	}

	//write by spy
	public synchronized  void CopytoRenderBuffer(long pointer, int width, int height) {
		if(mheight != height || mwidth != width)
		{
			need_glTex_init = true;
			mheight = height;
			mwidth = width;
		}

		long output = ConvertArry2Long(current_stream);
		CopyDataToBuffer(pointer, output, mwidth, mheight);
		return;
	}

	private native  long ConvertArry2Long(byte[] source);
	private native  long ClearMemory(byte[] source);
	private native boolean DrawData(long source, int width, int height, boolean need_init);
	private native boolean DrawByType(long source, int width, int height, int texturetype);
	private native boolean ConvertNV21(long source, long output, int width, int height);
	private native boolean ConvertYuv(long source, long output, int width, int height);
	private native boolean Convert2Normal(long source, int width, int height);
	private native boolean CopyDataToBuffer(long source, long output, int width, int height);
}
